package com.leonyr.smartipaddemo.crop;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.support.annotation.NonNull;

import com.leonyr.mvvm.vm.LViewModel;
import com.leonyr.smartipaddemo.adapter.PicBaseAdapter;
import com.leonyr.smartipaddemo.base.cro.CroHomeModel;
import com.leonyr.smartipaddemo.base.cro.IHomeView;
import com.leonyr.smartipaddemo.bean.PicItem;
import com.leonyr.smartipaddemo.view.DragGridView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Date： 2020/6/12 0012
 * Time： 15:23
 * Created by xtqb.
 */

public class CropHomeViewHolder<V extends IHomeView> extends LViewModel implements DragGridView.OnChangeListener {
    private PicBaseAdapter adapter;
    private CroHomeModel model;
    private List<PicItem> list;
    private V view;

    public void checkAllState(boolean state) {
        for (PicItem item : list) {
            if (state) {
                if (!item.isSelected()) {
                    item.setSelected(state);
                }
            } else {
                if (item.isSelected()) {
                    item.setSelected(state);
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    public PicBaseAdapter getAdapter() {
        if (adapter == null) {
            list = new ArrayList<>();
            adapter = new PicBaseAdapter(list);
        }
        return adapter;
    }

    public CropHomeViewHolder(@NonNull Context ctx) {
        super(ctx);
        model = new CroHomeModel();
        adapter = getAdapter();
    }

    public void reflesh(List<PicItem> itemList) {
//        if (list.size() > 0) {
//            list.clear();
//        }
        list.addAll(itemList);
        getAdapter().notifyDataSetChanged();
    }

    public void changeAllState(boolean state) {
        if (state) {
            for (PicItem item : list) {
                item.setVisible(true);
            }
        }
    }

    public List<String> getCompoudPaths(){
        List<String> paths = new ArrayList<>();
        for(PicItem item : list) {
            if(item.isSelected()) {
                paths.add(item.getPath());
            }
        }
        return paths;
    }

    public void setPicSelected(int pos){
        if(!list.get(pos).isSelected()) {
            list.get(pos).setSelected(true);
            adapter.notifyDataSetChanged();
        }
    }

    public String getPosPath(int pos){
        return list.get(pos).getPath();
    }

    public void delete(){
        int pos = list.size() -1;
        for(; pos > -1; pos --){
            if(list.get(pos).isSelected()) {
                list.remove(pos);
            }
        }
        adapter.notifyDataSetChanged();
    }

    public void checkAllVisible(boolean state) {
        for (PicItem item : list) {
            item.setVisible(state);
            item.setSelected(false);
        }
        adapter.notifyDataSetChanged();
    }


    public void hasCheckBox(boolean state) {
        model.hasCheckBox(state);
    }

    public void attachView(V view) {
        this.view = view;
    }

    public void dispose() {
        adapter = null;
        model.dispose();
        model = null;
        view = null;
    }

    @Override
    public void onChange(int from, int to) {
        if (from < to) {
            for (int i = from; i < to; i++) {
                Collections.swap(list, i, i + 1);
            }
        }else if (from > to) {
            for (int i = from; i > to; i--) {
                Collections.swap(list, i, i - 1);
            }
        }
        adapter.notifyDataSetChanged();
    }

    public void setRotateBit(int pos, int rotation) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.setRotate(rotation);
        Bitmap rotateBit = Bitmap.createBitmap(list.get(pos).getBitmap(), 0, 0,
                list.get(pos).getBitmap().getWidth(), list.get(pos).getBitmap().getHeight(),matrix, true);
        list.get(pos).setBitmap(rotateBit);
//        MineCameraActivity.saveBmpToFile(rotateBit,list.get(pos).getPath(),Bitmap.CompressFormat.JPEG);
        adapter.notifyDataSetChanged();
        saveImage(rotateBit, new File(list.get(pos).getPath()));
    }

    private void saveImage(Bitmap bitmap, File saveFile) {
        try {
            if (!saveFile.exists()) {
                saveFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(saveFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
